﻿/*
 * PsTFS is a set of PowerShell commands to manage and use Team Foundation Server. PowerShell 
 * effectiveness is combined with the power of TFS API to maximizes TFS capabilities.
 * 
 * Creator : Choulant Noham
 * Mail : choulant@gmail.com
 * Blog : http://choulant.blogspot.com
 * 
 */
using System;
using System.Linq;
using System.Management.Automation;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;
using PsTFS.Methodes;
using PsTFS.Objects;

namespace PsTFS.Commandes.Get
{
    /// <summary>
    /// Récupération des changesets
    /// </summary>
    [Cmdlet(VerbsCommon.Get, "SpecificVersion")]
    public class GetSpecificVersion : PsCmdlet
    {
        public GetSpecificVersion()
        {
            Date = DateTime.MinValue;
            Options = GetOptions.None;
        }

        /// <summary>
        /// Lancement du processus
        /// </summary>
        protected override void ProcessRecord()
        {
            try
            {
                VersionSpec vs = null;

                if (this.Source == null)
                    this.Source = string.Empty;
                
                if (this.Source.Contains('$'))
                    this.Source = this.Source;
                else
                    this.Source = "$/" + this.Source;

                if (this.Label != null)
                    vs = new LabelVersionSpec(this.Label);
                else if (this.Changeset > 0)
                    vs = new ChangesetVersionSpec(this.Changeset);
                else if (this.Date != DateTime.MinValue)
                    vs = new DateVersionSpec(this.Date);

                if (vs == null)
                    vs = VersionSpec.Latest;

                if (Workspace != null)
                    Context.Instance.Workspace = Workspace;

                Methode.GetVersion(Source, vs, Options);
            }
            catch (Exception ex)
            {
                ErrorRecord err = ErrorRecord(ex, ErrorCategory.WriteError);
                WriteError(err);
                Log.AddLog(ex.StackTrace);
            }
            finally
            {
                base.ProcessRecord();
            }

        }


        /// <summary>
        /// Permet d'informer la fin de la commande
        /// </summary>
        protected override void EndProcessing()
        {
            Log.AddLog(null, "Get-SpecificVersion is complete");
            base.EndProcessing();
        }

        /// <summary>
        /// Object contenant Team Foundation Server
        /// </summary>
        [Parameter(Position = 0)]
        public TfsTeamProjectCollection TFS
        {
            set { Context.Instance.TFS = value; }
        }

        /// <summary>
        /// Projet Source
        /// </summary>
        [Parameter(Position = 1)]
        public String Source { get; set; }
        
        /// <summary>
        /// Label à la quel récupérer la source
        /// </summary>
        [Parameter(Position = 2)]
        public String Label { get; set; }

        /// <summary>
        /// Changeset à la quel récupérer la source
        /// </summary>
        [Parameter(Position = 3)]
        public int Changeset { get; set; }

        /// <summary>
        /// Date à la quel récupérer la source
        /// </summary>
        [Parameter(Position = 4)]
        public DateTime Date { get; set; }

        [Parameter(Position = 5)]
        [ValidateNotNullOrEmpty]
        public Workspace Workspace { get; set; }

        [Parameter(Position = 6)]
        public GetOptions Options { get; set; }
    }
}